<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/4/22
 * Time: 10:38
 */

namespace app\api\controller;

use app\model\StaffModel;
use app\model\StaffRoleModel;
use app\model\StaffJurisdictionModel;
use app\model\LeftMenuModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Login
{

     public function login()
    {
        if(Request::param('token')) {
            session_id(Request::param('token'));
        }
        session_start();
        $params = Request::param();

        $validate = Validate::rule([
            'username|员工用户名' => 'require',
            'password|员工密码' => 'require',
            'verify_code|验证码' => 'require|min:4|max:4',
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        if ($_SESSION['verify_code'] != $params['verify_code']) {
            $data = [
                'status' => 1002,
                'message' => '验证码不对',
                'data' => [
                    'result' => [],
                ],
            ];
            return json($data);
        }

        $staff = StaffModel::where("username", '=', $params['username'])->find();
        $result = false;
        if ($staff && StaffModel::checkPassword($params['password'], $staff['password'])) {
            session("username", $staff['username']);
//            session("admin_name", $admin['admin_name']);
            //员工角色
            $staffRoleOne = StaffRoleModel::find($staff['role_id']);
            $staff['role_name'] = $staffRoleOne['role_name'];
            $staffJurisdiction = StaffJurisdictionModel::where('role_id','=',$staff['role_id'])->select();
            $leftMenuName = [];
            foreach ($staffJurisdiction as &$value){
            //菜单名称
            $leftMenuOne = LeftMenuModel::find($value['left_menu_id']);
            $leftMenuName[] = $leftMenuOne['left_menu_name'];
        }
        unset($value);
            $result = true;
        }


        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'staffInfo' =>$result ? $staff : false,
                'leftMenuName' => $result ? $leftMenuName : false
            ]
        ];
        return json($data);
    }

    public function logout()
    {
        session("admin_name", null);
//        session("admin_email", null);

        $data = [
            'status' => session('admin_name') ? 10001 : 0,
            'message' => session('admin_name') ? '修改数据库失败' : '',
            'data' => [
                'result' =>session('admin_name') ? false : true
            ]
        ];
        return json($data);
    }

     public function verifyCode()
    {
        session_start();

        $image = \imagecreate(58, 25); // 创建一张图片
        $white = \imagecolorallocate($image, 255, 255, 255);// 创建背景为白色

        $verifyCode = '';
        for ($i = 1; $i <= 4; $i++) {
            $str = mt_rand(0, 9); // 要插入的字符串
            $font = 4; // 字体大小，如果 font 是 1，2，3，4 或 5，则使用内置字体。
            $x = 2 + ($i - 1) * 15; // 左偏移量，每画1个，左偏移量就加大一些。
            $y = 5; // 上偏移量
            $color = \imagecolorallocate($image, 255, 192, 203); // 设置一个浅粉色
            \imagestring($image, $font, $x, $y, $str, $color); // 将字符串画到图片上

            $verifyCode .= $str;
        }
        $_SESSION['verify_code'] = $verifyCode;
//        header("Content-Type:","image/png");
        ob_start();
        \imagepng($image); // 以 PNG 格式将图像输出到浏览器
        $content = ob_get_clean();
        \imagedestroy($image); // 销毁一图像， 释放资源
//        echo "<br />";
        $imageSource =  'data:image/png;base64,'.base64_encode($content);

        $data = [
            'imageSource' => $imageSource,
            'token' => session_id()
        ];

        return json($data);
    }
}